/**
 * token 信息，存储如应用的认证信息
 * @param app
 * @returns {*}
 */
module.exports = app => {
  const mongoose = app.mongoose
  const { Schema } = mongoose
  const { ObjectId } = Schema

  const modelSchema = new Schema({
    name: { type: String, remark: '用途信息', required: true },
    accessKey: { type: String, require: true, remark: '密钥' },
    accessToken: { type: String, require: true, remark: 'token' },
    user: { type: ObjectId, required: true, ref: 'user', remark: '关联用户' },
    spm: { type: Number, default: 0, enum: { 0: '用户认证', 1: '服务器认证', 2: '外部应用' }, remark: '用途' },
    expiredAt: { type: Number, remark: '到期时间' },
  }, {
    collections: 'application',
    timestamps: true,
  })

  modelSchema.index({ name: 1 }, { unique: true })
  modelSchema.index({ accessKey: 1 }, { unique: true })
  modelSchema.index({ accessToken: 1 })

  return mongoose.model('application', modelSchema)
}